﻿using System;
using OctoOS;

/// <summary>
/// Summary description for Urls
/// </summary>
public static class Urls
{
	public const string Root = "/";
	public const string Home = "tilde/";
	
	public static class Admin
	{
		public const string AdminHome = Root + Home + "admin/";

		public static string GetDefault()
		{
			return AdminHome + "default.aspx";
		}

		public static string GetEditCategory(long categoryID)
		{
			return AdminHome + "editCategory.aspx/" + ConvertUtility.ToString(categoryID, 64);
		}

		public static string GetAddCategory()
		{
			return AdminHome + "editCategory.aspx";
		}

		public static string GetDeleteCategory(long categoryID)
		{
			return AdminHome + "deleteCategory.aspx/" + ConvertUtility.ToString(categoryID, 64);
		}
	}

	public static class Ajax
	{
		public const string AjaxHome = Home + "AjaxSectionService.svc/";

		public static string GetSaveSection()
		{
			return Root + AjaxHome + "SaveHtml";
		}

		public static string GetCategoryRss(long categoryID, bool root)
		{
			var path = String.Format(AjaxHome + "GetRss?category={0}", categoryID);

			if(root)
				return Root + path;
			else
				return path;
		}

		public static string GetMainRss(bool root)
		{
			var path = AjaxHome + "GetMainRss";

			if(root)
				return Root + path;
			else
				return path;
		}

		public static string GetSavePagesOrder()
		{
			return AjaxHome + "SavePagesOrder";
		}
		
		public static string GetSaveSectionsOrder()
		{
			return AjaxHome + "SaveSectionsOrder";
		}

		public static string GetDeleteSection()
		{
			return AjaxHome + "DeleteSection";
		}

		public static string GetDeletePage()
		{
			return AjaxHome + "DeletePage";
		}
	}

	public static string GetEditArticle(long id)
	{
		return Root + Home + "editArticle.aspx/" + ConvertUtility.ToString(id, 64);
	}

	public static string GetAddArticle()
	{
		return Root + Home + "editArticle.aspx";
	}


	public static string GetAddSection(long articleID, long pageID)
	{
		return Root + Home + "editSection.aspx/" + ConvertUtility.ToString(articleID, 64) + "/" + pageID;
	}

	public static string GetAddXhtmlSection(long articleID, long pageID)
	{
		return Root + Home + "editSection.aspx/" + ConvertUtility.ToString(articleID, 64) + 
			"/" + pageID + "/0/" + TextFormat.Html.ToString().ToLower();
	}

	public static string GetAddBBCodeSection(long articleID, long pageID)
	{
		return Root + Home + "editSection.aspx/" + ConvertUtility.ToString(articleID, 64) + 
			"/" + pageID + "/0/" + TextFormat.BBCode.ToString().ToLower();
	}

	public static string GetAddPlainSection(long articleID, long pageID)
	{
		return Root + Home + "editSection.aspx/" + ConvertUtility.ToString(articleID, 64) + 
			"/" + pageID + "/0/" + TextFormat.Plain.ToString().ToLower();
	}

	public static string GetEditSection(long articleID, long pageID, long id)
	{
		return Root + Home + "editSection.aspx/" + ConvertUtility.ToString(articleID, 64) + 
			"/" + pageID + "/" + id;
	}

	public static string GetAddPage(long articleID)
	{
		return Root + Home + "editPage.aspx/" + ConvertUtility.ToString(articleID, 64);
	}

	public static string GetEditPage(long articleID, long pageID)
	{
		return Root + Home + "editPage.aspx/" + ConvertUtility.ToString(articleID, 64)
			+ "/" + pageID;
	}

	public static string GetMyArticles()
	{
		return Root + Home + "myArticles.aspx";
	}

	public static string GetDeletePage(long articleID, long pageID)
	{
		return Root + Home + "deletePage.aspx/" + ConvertUtility.ToString(articleID, 64) + "/" + pageID;
	}

	public static string GetArticle(long articleID, bool admin, bool encode, string name)
	{
		string amp = encode ? "&amp;" : "&";

		return Root + Home + "article.aspx?id=" + ConvertUtility.ToString(articleID, 64) +
			amp + "name=" + GetEncodedName(name) +
			(admin ? amp + "mode=admin" : string.Empty);
	}

	public static string GetArticle(long articleID, bool admin, bool encode, long pageID,
		string name)
	{
		string amp = encode ? "&amp;" : "&";
		return GetArticle(articleID, admin, encode, name) + amp + "page=" + pageID;
	}

	public static string GetCategory(long categoryID, string title)
	{
		return Root + Home + "category.aspx/" + ConvertUtility.ToString(categoryID, 64) + "/" + 
			AppUtil.GetEncodedTitle(title);
	}

	public static string GetDeleteArticle(long articleID)
	{
		return Root + Home + "deleteArticle.aspx/" + ConvertUtility.ToString(articleID, 64);
	}

	
	public static string GetDeleteSection(long articleID, long pageID, long sectionID)
	{
		return Root + Home + "deleteSection.aspx/" + ConvertUtility.ToString(articleID, 64)
			+ "/" + pageID + "/" + sectionID;
	}

	public static string GetPostRedirectUrl(long threadID, long postID)
	{
		return Root + Home + "post.aspx?thread=" + threadID + "&post=" + postID;
	}

	public static string GetArticleDiscussion(long id, string title,
		int pageIndex, long postID)
	{
		string url = Root + Home + "article.aspx?id=" + ConvertUtility.ToString(id, 64) + "&name=" + GetEncodedName(title);

		if(pageIndex > 0)
			url += "&cpage=" + pageIndex;

		if(0 != postID)
			url += "#p" + postID;

		return url;
	}

	public static string GetEncodedName(string name)
	{
		return HtmlUtility.UrlPathLevelEncode(name, 80, true);
	}

	public static string GetAllArticles()
	{
		return Root + Home + "allArticles.aspx";
	}
}